package com.koron.sinoprobe.web.purchase.service.impl;


import com.koron.sinoprobe.web.purchase.bean.po.SpPurchaseGoods;
import com.koron.sinoprobe.web.purchase.mapper.SpPurchaseGoodsMapper;
import com.koron.sinoprobe.web.purchase.service.SpPurchaseGoodsService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购验收-物品清单(SpPurchaseGoods)表服务实现类
 *
 * @author jzp
 * @since 2021-12-29 10:20:04
 */
@Service
public class SpPurchaseGoodsServiceImpl implements SpPurchaseGoodsService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpPurchaseGoods getById(SessionFactory factory, String id) {
        SpPurchaseGoodsMapper mapper = factory.getMapper(SpPurchaseGoodsMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpPurchaseGoods> list(SessionFactory factory, SpPurchaseGoods spPurchaseGoods) {
        SpPurchaseGoodsMapper mapper = factory.getMapper(SpPurchaseGoodsMapper.class);
        return mapper.selectList(spPurchaseGoods);
    }

    @Override
    @TaskAnnotation("listByAcceptId")
    public List<SpPurchaseGoods> listByAcceptId(SessionFactory factory, String acceptId) {
        SpPurchaseGoods spPurchaseGoods = new SpPurchaseGoods();
        spPurchaseGoods.setAcceptId(acceptId);
        return this.list(factory, spPurchaseGoods);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, SpPurchaseGoods spPurchaseGoods) {
        SpPurchaseGoodsMapper mapper = factory.getMapper(SpPurchaseGoodsMapper.class);
        return mapper.insertOrUpdate(spPurchaseGoods) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<SpPurchaseGoods> lists) {
        if (CollectionUtils.isEmpty(lists)) {
            return false;
        }
        SpPurchaseGoodsMapper mapper = factory.getMapper(SpPurchaseGoodsMapper.class);
        return mapper.insertOrUpdateBatch(lists) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpPurchaseGoods spPurchaseGoods) {
        SpPurchaseGoodsMapper mapper = factory.getMapper(SpPurchaseGoodsMapper.class);
        return mapper.update(spPurchaseGoods) > 0;
    }


    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        SpPurchaseGoodsMapper mapper = factory.getMapper(SpPurchaseGoodsMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByAcceptId")
    public boolean removeByAcceptId(SessionFactory factory, String acceptId) {
        if (StringUtils.isEmpty(acceptId)) {
            return false;
        }
        List<SpPurchaseGoods> spPurchaseGoods = listByAcceptId(factory, acceptId);
        return deleteBatchIds(factory, spPurchaseGoods.stream().map(SpPurchaseGoods::getId).collect(Collectors.toList()));

    }
}
